package com.taru.wxhsh.controller;

import com.taru.wxhsh.common.JsonResult;
import com.taru.wxhsh.constant.JResponseConstants;
import com.taru.wxhsh.pojo.Category;
import com.taru.wxhsh.service.CategoryService;
import com.taru.wxhsh.util.UidGenerators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by HuKang on 2018/5/30.
 */
@Controller
public class CategoryController {
    Logger logger  = LoggerFactory.getLogger(CategoryController.class);
    @Autowired
    CategoryService categoryService;

    /**
     * 查询类别列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/category/qryList")
    public Object qryList(){
        JsonResult<List<Category>> result = null;
        try {
            List<Category> list = categoryService.qryList();
            result = new JsonResult<List<Category>>(JResponseConstants.STATUS_OK,"查询类别成功",list);
        }catch (Exception ex){
            logger.error("查询类别异常：{}",ex.getMessage());
            result = new JsonResult<List<Category>>(JResponseConstants.STATUS_ERROR,"查询类别异常");
        }

        return  result;
    }

    /**
     * 删除类别
     * @param categoryId
     * @return
     */
    @ResponseBody
    @RequestMapping("/category/delById")
    public Object delById(String categoryId){
        JsonResult<String> result = null;
        try {
            categoryService.deleteByPrimaryKey(categoryId);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"删除类别成功");
        }catch (Exception ex){
            logger.error("删除类别异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"删除类别异常");
        }
        return result;
    }

    /**
     * 添加类别
     * @param category
     * @return
     */
    @ResponseBody
    @RequestMapping("/category/add")
    public Object categoryAdd(Category category){
        JsonResult<String> result = null;
        try {
            category.setCategoryId(UidGenerators.getUuid());
            categoryService.insertSelective(category);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"添加类别成功");
        }catch (Exception ex){
            logger.error("添加类别异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"添加类别异常");
        }
        return result;
    }

    /**
     * 修改类别排序数字
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/category/updateById")
    public Object updateById(String categoryId){
        JsonResult<String> result = null;
        try {
            categoryService.upByPrimaryKey(categoryId);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"修改类别数字成功");
        }catch (Exception ex){
            logger.error("修改类别数字异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"修改类别数字异常");
        }
        return result;
    }

    /**
     * 修改类别排序数字
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/category/downdateById")
    public Object downdateById(String categoryId){
        JsonResult<String> result = null;
        try {
            categoryService.downByPrimaryKey(categoryId);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"修改类别数字成功");
        }catch (Exception ex){
            logger.error("修改类别数字异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"修改类别数字异常");
        }
        return result;
    }



    /**
     * 修改全部信息
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/category/update")
    public Object update(Category category){
        JsonResult<String> result = null;
        try {
            categoryService.updateCategory(category);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"修改信息成功");
        }catch (Exception ex){
            logger.error("修改信息异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"修改信息异常");
        }
        return result;
    }


    /**
     * 修改全部信息
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/category/selectById")
    public Object selectById(String categoryId){
        JsonResult<Category> result = null;
        try {
           Category category= categoryService.selectById(categoryId);
            result = new JsonResult<Category>(JResponseConstants.STATUS_OK,"查询类别信息成功",category);
        }catch (Exception ex){
            logger.error("查询类别信息异常：{}",ex.getMessage());
            result = new JsonResult<Category>(JResponseConstants.STATUS_ERROR,"查询类别信息异常");
        }
        return result;
    }



}
